package com.herench.bs.handler;

import com.herench.bs.agent.IOutpatientClinicAgent;
import com.herench.bs.annotation.Handler;
import com.herench.bs.support.AbstractMessageHandler;
import com.herench.bs.support.RequestMessage;
import com.herench.bs.support.ResponseMessage;
import com.herench.bs.utils.DataFormatUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Handler(code = "101010" ,name="检查检验总列表查询")
//@Handler(code = "10" ,name="检查检验总列表查询")
public class LoadReportListHandler extends AbstractMessageHandler {
    @Override
    protected ResponseMessage innerHandler(RequestMessage requestMessage) throws Exception {
        ResponseMessage responseMessage = new ResponseMessage(requestMessage.getId());
        String startDate = requestMessage.getArgsPropertyStringValue("startDate");
        Assert.notNull(startDate, "startDate 不可为空！");
        String endDate = requestMessage.getArgsPropertyStringValue("endDate");
        Assert.notNull(endDate, "endDate 不可为空！");
//        String cardNo = requestMessage.getArgsPropertyStringValue("cardNo");
        String cardNo = requestMessage.getArgsPropertyStringValue("patientId");
        Assert.notNull(cardNo, "cardNo 不可为空！");
        String reportType = requestMessage.getArgsPropertyStringValue("reportType");
        Assert.notNull(reportType, "reportType 不可为空！");
        IOutpatientClinicAgent outpatientClinicAgent = getCleverClinicContext().getApplicationContext().getBean(IOutpatientClinicAgent.class);
        if (ReportType.EXAM.getType().equals(reportType)) {
            Map<String, Object> res = outpatientClinicAgent.loadExamList(startDate, endDate, cardNo);
            List<Map<String, Object>> dataList = (List<Map<String, Object>>) res.get("dataList");
            List<ItemElement> elements = new ArrayList<>();
            Map<String, Object> finalResMap = new HashMap<>();
            if(dataList != null){
                for (Map<String, Object> map : dataList) {
                    ItemElement itemElement = new ItemElement();
                    itemElement.setType(ReportType.EXAM.getType());
                    itemElement.setDeptName((String) map.get("deptName"));
                    itemElement.setDocName((String) map.get("docName"));
                    itemElement.setRepId((String) map.get("repId"));
                    itemElement.setRepName((String) map.get("repName"));
                    itemElement.setRepTime((String) map.get("repTime"));
                    itemElement.setRegTime((String) map.get("regTime"));
                    elements.add(itemElement);
                }

            }
            finalResMap.put("dataList", shuff(elements));
            responseMessage.setData(finalResMap);
            getPropertiesFromInvokerResultMap(responseMessage, res);
        }
        if (ReportType.LIS.getType().equals(reportType)) {
            Map<String, Object> res = outpatientClinicAgent.loadLabMasterList(startDate, endDate, cardNo);
            List<Map<String, Object>> dataList = (List<Map<String, Object>>) res.get("dataList");
            List<ItemElement> elements = new ArrayList<>();
            Map<String, Object> finalResMap = new HashMap<>();
            if(dataList != null){
                for (Map<String, Object> map : dataList) {
                    ItemElement itemElement = new ItemElement();
                    itemElement.setType(ReportType.LIS.getType());
                    itemElement.setDeptName((String) map.get("deptName"));
                    itemElement.setDocName((String) map.get("docName"));
                    itemElement.setRepId((String) map.get("repId"));
                    itemElement.setRepName((String) map.get("repName"));
                    itemElement.setRepTime((String) map.get("repTime"));
                    itemElement.setRegTime((String) map.get("regTime"));
                    elements.add(itemElement);
                }
            }
            finalResMap.put("dataList", shuff(elements));

            responseMessage.setData(finalResMap);
            getPropertiesFromInvokerResultMap(responseMessage, res);
        }
        if (ReportType.ALL.getType().equals(reportType)) {
            Map<String, Object> res = outpatientClinicAgent.loadExamList(startDate, endDate, cardNo);
            Map<String, Object> res1 = outpatientClinicAgent.loadLabMasterList(startDate, endDate, cardNo);
//            Assert.isTrue(res.get("code").equals(res1.get("code")) && res.get("code").equals("0"), "查询失败");
            Map<String, Object> finalResMap = new HashMap<>();
            finalResMap.put("code", "0");
            finalResMap.put("message", "查询成功");
            List<Map<String, Object>> dataList1 = (List<Map<String, Object>>) res.get("dataList");
            List<Map<String, Object>> dataList2 = (List<Map<String, Object>>) res1.get("dataList");
            List<ItemElement> elements = new ArrayList<>();
            if(dataList1 != null){
                for (Map<String, Object> map : dataList1) {
                    ItemElement itemElement = new ItemElement();
                    itemElement.setType(ReportType.EXAM.getType());
                    itemElement.setDeptName((String) map.get("deptName"));
                    itemElement.setDocName((String) map.get("docName"));
                    itemElement.setRepId((String) map.get("repId"));
                    itemElement.setRepName((String) map.get("repName"));
                    itemElement.setRepTime((String) map.get("repTime"));
                    itemElement.setRegTime((String) map.get("regTime"));
                    elements.add(itemElement);
                }
            }

            if(dataList2!= null){
                for (Map<String, Object> map : dataList2) {
                    ItemElement itemElement = new ItemElement();
                    itemElement.setType(ReportType.LIS.getType());
                    itemElement.setDeptName((String) map.get("deptName"));
                    itemElement.setDocName((String) map.get("docName"));
                    itemElement.setRepId((String) map.get("repId"));
                    itemElement.setRepName((String) map.get("repName"));
                    itemElement.setRepTime((String) map.get("repTime"));
                    itemElement.setRegTime((String) map.get("regTime"));
                    elements.add(itemElement);
                }
            }
            finalResMap.put("dataList", shuff(elements));
            responseMessage.setData(finalResMap);
            getPropertiesFromInvokerResultMap(responseMessage, finalResMap);
        }
//        }
        return responseMessage;
    }

    private enum ReportType {
        EXAM("1", "检查"), LIS("2", "检验"), ALL("3", "全部");

        ReportType(String type, String name) {
            this.type = type;
            this.name = name;
        }

        private String type;
        private String name;

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    private class ItemElement {

        private String type;
        private String repTime;
        private String deptName;
        private String repId;
        private String repName;
        private String docName;
        private String regTime;

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getRepTime() {
            return repTime;
        }

        public void setRepTime(String repTime) {
            this.repTime = repTime;
        }

        public String getDeptName() {
            return deptName;
        }

        public void setDeptName(String deptName) {
            this.deptName = deptName;
        }

        public String getRepId() {
            return repId;
        }

        public void setRepId(String repId) {
            this.repId = repId;
        }

        public String getRepName() {
            return repName;
        }

        public void setRepName(String repName) {
            this.repName = repName;
        }

        public String getDocName() {
            return docName;
        }

        public void setDocName(String docName) {
            this.docName = docName;
        }

        public String getRegTime() {
            return regTime;
        }

        public void setRegTime(String regTime) {
            this.regTime = regTime;
        }

    }


    private List<Map<String, Object>> shuff(List<ItemElement> elements) throws Exception{
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> temp = new HashMap<>();
        for (ItemElement element : elements)
        {
            String key = DataFormatUtils.getDayString(element.getRepTime());
            List<ItemElement> subList = (List<ItemElement>) temp.get(key);
            if (subList == null) {
                subList = new ArrayList<>();
                temp.put(key, subList);
            }
            subList.add(element);
        }
        for (Map.Entry<String, Object> el : temp.entrySet()) {
            Map<String, Object> elMap = new HashMap<>();
            elMap.put("time", el.getKey());
            elMap.put("items", el.getValue());
            list.add(elMap);
        }
        return list;
    }

}

